//
//  ChannelVideoCell.m
//  Channel
//
//  Created by CPX on 2018/9/5.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "ChannelVideoCell.h"

/**
 视频Cell
 @author yzl
 */
@interface ChannelVideoCell()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *videoImageV;
@property (nonatomic, strong) UIImageView *playImageV;
@property (nonatomic, strong) UILabel *HDLabel;
@property (nonatomic, strong) UILabel *LIVELabel;
//@property (nonatomic, strong) UIButton *titleBtn;
//@property (nonatomic, strong) UIButton *viewBtn;

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *authorNameLabel;
@property (nonatomic, strong) UIImageView *viewImageV;
@property (nonatomic, strong) UILabel *viewTimesLabel;
@property (nonatomic, strong) UILabel *videoTitleL;

@end

@implementation ChannelVideoCell

- (void)setModel:(ChannelVideoModel *)model{
    _model = model;
    
    [self.videoImageV sd_setImageWithURL:[NSURL URLWithString:model.video_thumbnail] placeholderImage:[UIImage imageNamed:@"place_holder2"]];
    if ([model.sharpness isEqualToString:@"LIVE"]) {
        self.LIVELabel.backgroundColor = UIColorHex(#2B9958);
        self.HDLabel.backgroundColor = UIColorHex(#0000009a);
    }
    else{
        self.HDLabel.backgroundColor = UIColorHex(#2B9958);
        self.LIVELabel.backgroundColor = UIColorHex(#0000009a);
    }
    self.playImageV.hidden = NO;
    
    if (model.video_author.count) {
        ChannelVideoAuthor *author = model.video_author[0];
        self.authorNameLabel.text = author.name;
        [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:author.avatar] placeholderImage:[UIImage imageNamed:@"no_photo"]];
    }
    else{
        [self.avatarImageV sd_cancelCurrentAnimationImagesLoad];
        self.avatarImageV.image = [UIImage imageNamed:@"no_photo"];
        self.authorNameLabel.text = @"";
    }
    self.videoTitleL.text = model.video_name;
    self.viewTimesLabel.text = [NSString stringWithFormat:@"%ld",model.cnt_watch];
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]) {
        self.viewTimesLabel.text = self.viewTimesLabel.text.arabicString;
    }
    self.viewImageV.hidden = NO;
}

#pragma mark - lazy load

- (UIView*)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 5;
        _bgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_bgView];
        self.contentView.backgroundColor = UIColorHex(#F6F6F6);
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.mas_equalTo(12);
            make.trailing.mas_equalTo(-12);
            make.bottom.mas_equalTo(0);
        }];
    }
    return _bgView;
}

- (UIImageView*)videoImageV{
    if(!_videoImageV){
        _videoImageV = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.bgView addSubview:_videoImageV];
        [_videoImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.mas_equalTo(12);
            make.trailing.mas_equalTo(-12);
            make.bottom.mas_equalTo(-48);
        }];
    }
    return _videoImageV;
}

- (UIImageView*)playImageV{
    if(!_playImageV){
        _playImageV = [[UIImageView alloc] initWithFrame:CGRectZero];
        _playImageV.image = [UIImage imageNamed:@"play_btn"];
        [self.videoImageV addSubview:_playImageV];
        [_playImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.videoImageV);
        }];
    }
    return _playImageV;
}

- (UILabel*)HDLabel{
    if(!_HDLabel){
        _HDLabel = [UILabel new];
        _HDLabel.text = @"HD";
        _HDLabel.textColor = [UIColor whiteColor];
        _HDLabel.font = [UIFont systemFontOfSize:10];
        _HDLabel.layer.masksToBounds = YES;
        _HDLabel.layer.cornerRadius = 7.5;
        _HDLabel.textAlignment = NSTextAlignmentCenter;
        [self.videoImageV addSubview:_HDLabel];
        [_HDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(-56);
            make.top.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(34, 15));
        }];
    }
    return _HDLabel;
}

- (UILabel*)LIVELabel{
    if(!_LIVELabel){
        _LIVELabel = [UILabel new];
        _LIVELabel.text = TLString(@"LIVE");
        _LIVELabel.font = [UIFont systemFontOfSize:10];
        _LIVELabel.textAlignment = NSTextAlignmentCenter;
        _LIVELabel.layer.masksToBounds = YES;
        _LIVELabel.layer.cornerRadius = 7.5;
        _LIVELabel.textColor = [UIColor whiteColor];
        [self.videoImageV addSubview:_LIVELabel];
        [_LIVELabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(-14);
            make.top.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(34, 15));
        }];
    }
    return _LIVELabel;
}

//- (UIButton*)titleBtn{
//    if (!_titleBtn) {
//        _titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_titleBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
//        _titleBtn.userInteractionEnabled = NO;
//        _titleBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 7);
//        [self.bgView addSubview:_titleBtn];
//        [_titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.leading.mas_equalTo(12);
//            make.bottom.mas_equalTo(-12);
//            make.height.mas_equalTo(30);
//            make.width.mas_greaterThanOrEqualTo(10);
//        }];
//    }
//    return _titleBtn;
//}
//
//- (UIButton*)viewBtn{
//    if (!_viewBtn) {
//        _viewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_viewBtn setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
//        _viewBtn.userInteractionEnabled = NO;
//        _viewBtn.titleLabel.font = [UIFont systemFontOfSize:13];
//        [_viewBtn setImage:[UIImage imageNamed:@"preview_ic"] forState:UIControlStateNormal];
//        _viewBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
//        [self.bgView addSubview:_viewBtn];
//        [_viewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.trailing.mas_equalTo(-12);
//            make.bottom.mas_equalTo(-10);
//            make.height.mas_equalTo(25);
//            make.width.mas_greaterThanOrEqualTo(10);
//        }];
//    }
//    return _viewBtn;
//}

- (UIImageView*)avatarImageV{
    if (!_avatarImageV) {
        _avatarImageV = [UIImageView new];
        _avatarImageV.layer.masksToBounds = YES;
        _avatarImageV.layer.cornerRadius = 12;
        [self.bgView addSubview:_avatarImageV];
        [_avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(24);
            make.bottom.mas_equalTo(-12);
            make.leading.mas_equalTo(12);
        }];
    }
    return _avatarImageV;
}

- (UIImageView*)viewImageV{
    if (!_viewImageV) {
        _viewImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"preview_ic"]];
        
        [self.bgView addSubview:_viewImageV];
        [_viewImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.mas_equalTo(self.avatarImageV); make.trailing.mas_equalTo(self.viewTimesLabel.mas_leading).offset(-6);
        }];
    }
    return _viewImageV;
}

- (UILabel*)authorNameLabel{
    if(!_authorNameLabel){
        _authorNameLabel = [UILabel new];
        
        _authorNameLabel.textColor = UIColorHex(#525252);
        _authorNameLabel.font = [UIFont boldSystemFontOfSize:14];
        [self.bgView addSubview:_authorNameLabel];
        [_authorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_greaterThanOrEqualTo(10);
            make.centerY.mas_equalTo(self.avatarImageV);
            make.leading.mas_equalTo(self.avatarImageV.mas_trailing).offset(6.5);
        }];
    }
    return _authorNameLabel;
}

- (UILabel*)videoTitleL{
    if(!_videoTitleL){
        _videoTitleL = [UILabel new];
        
        _videoTitleL.textColor = UIColorHex(#ffffff);
        _videoTitleL.numberOfLines = 0;
        _videoTitleL.font = [UIFont boldSystemFontOfSize:14];
        [self.bgView addSubview:_videoTitleL];
        [_videoTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
            make.bottom.mas_equalTo(self.videoImageV.mas_bottom).offset(-5); make.leading.mas_equalTo(self.videoImageV.mas_leading).offset(5);
            make.trailing.mas_equalTo(self.videoImageV.mas_trailing).offset(-5);
        }];
    }
    return _videoTitleL;
}

- (UILabel*)viewTimesLabel{
    if(!_viewTimesLabel){
        _viewTimesLabel = [UILabel new];
        
        _viewTimesLabel.textColor = UIColorHex(#6F6F6F);
        _viewTimesLabel.font = [UIFont systemFontOfSize:13];
        [self.bgView addSubview:_viewTimesLabel];
        [_viewTimesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_greaterThanOrEqualTo(10);
            make.centerY.mas_equalTo(self.avatarImageV);
            make.trailing.mas_equalTo(-12);
        }];
    }
    return _viewTimesLabel;
}

@end
